package com.milo.reactor.controller;

import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.ArrayList;
import java.util.stream.Stream;

/**
 * @author Milo Lee
 * @date 2021-12-07 10:08
 */
public class FluxTest {

    /***
     * just
     * @author Milo Lee
     * @date 2021-12-07 10:24
     */
    @Test
    public void createFluxJust(){
        //发布者
        Flux<String> fruitFlux = Flux.just("Apple", "Orange", "Grape", "Banana", "Straw");

        //断言判断值
        StepVerifier.create(fruitFlux)
                .expectNext("Apple")
                .expectNext("Orange")
                .expectNext("Grape")
                .expectNext("Banana")
                .expectNext("Straw")
                .verifyComplete();
        //订阅者
        fruitFlux.subscribe(f -> System.out.println(f));

    }

    /***
     * fromArray
     * @author Milo Lee
     * @date 2021-12-07 10:23
     */
    @Test
    public void createFluxFromArray(){
        String[] strings = {"Apple", "Orange", "Grape", "Banana", "Straw"};
        Flux<String> fruitFlux = Flux.fromArray(strings);
        //断言判断值
        StepVerifier.create(fruitFlux)
                .expectNext("Apple")
                .expectNext("Orange")
                .expectNext("Grape")
                .expectNext("Banana")
                .expectNext("Straw")
                .verifyComplete();
    }

    /***
     * fromIterable
     * @author Milo Lee
     * @date 2021-12-07 10:25
     */
    @Test
    public void createFluxFromIterable(){
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Orange");
        arrayList.add("Grape");
        arrayList.add("Banana");
        arrayList.add("Straw");
        Flux<String> fruitFlux = Flux.fromIterable(arrayList);

        //断言判断值
        StepVerifier.create(fruitFlux)
                .expectNext("Apple")
                .expectNext("Orange")
                .expectNext("Grape")
                .expectNext("Banana")
                .expectNext("Straw")
                .verifyComplete();
    }

    /***
     * fromStream
     * @author Milo Lee
     * @date 2021-12-07 10:30
     */
    @Test
    public void createFluxFromStream(){
        Stream<String> apple = Stream.of("Apple", "Orange", "Grape", "Banana", "Straw");
        Flux<String> fruitFlux = Flux.fromStream(apple);

        //断言判断值
        StepVerifier.create(fruitFlux)
                .expectNext("Apple")
                .expectNext("Orange")
                .expectNext("Grape")
                .expectNext("Banana")
                .expectNext("Straw")
                .verifyComplete();
    }
    
    /***
     * interval
     * @author Milo Lee
     * @date 2021-12-07 11:51 
     */
    @Test
    public void createFluxInterval(){
        Flux<Long> take = Flux.interval(Duration.ofSeconds(1)).take(5);

        //断言判断值
        StepVerifier.create(take)
                .expectNext(0L)
                .expectNext(1L)
                .expectNext(2L)
                .expectNext(3L)
                .expectNext(4L)
                .verifyComplete();
    }

    /***
     * mergeFluxes 不能完美保证顺序
     * @author Milo Lee
     * @date 2021-12-07 11:55
     */
    @Test
    public void mergeFluxes(){
        Flux<String> just = Flux.just("Garfield", "kojka", "Barbossa")
                .delayElements(Duration.ofMillis(500));



        Flux<String> footFlux = Flux.just("Lasa", "Loll", "Apples")
                .delaySubscription(Duration.ofMillis(250))
                .delayElements(Duration.ofMillis(500));

        Flux<String> stringFlux = just.mergeWith(footFlux);

        //断言判断值
        StepVerifier.create(stringFlux)
                .expectNext("Garfield")
                .expectNext("Lasa")
                .expectNext("kojka")
                .expectNext("Loll")
                .expectNext("Barbossa")
                .expectNext("Apples")
                .verifyComplete();
    }
}
